<template>
  <div>
    <el-form 
			class="elForm" 
			:inline="true" 
			:model="dataForm" 
			:rules="dataRule" 
			ref="dataForm"
			>
      <el-form-item>
        <el-radio-group v-model="status">
          <el-radio label="待审批"></el-radio>
          <el-radio label="已完成"></el-radio>
          <el-radio label="全部"></el-radio>
        </el-radio-group>
        <el-button 
          class="add-btn" 
          size="small" 
          style="margin-left: 20px"
          @click="loadDataList()">
          查询
        </el-button>
      </el-form-item>
      <el-form-item prop="examName">
        <el-input v-model="dataForm.title" placeholder="考核名称" size="medium" class="input" clearable="clearable" />
        <el-button class="search" size="small" @click="searchHandle()" icon="el-icon-search"></el-button>
      </el-form-item>
    </el-form>
    <el-table 
			:data="dataList"
      ref="dataList"
			border 
			v-loading="dataListLoading"
			@selection-change="selectionChangeHandle" 
			style="width: 100%;" size="medium" 
			:header-cell-style="{borderColor: 'rgb(235,235,235)',background: 'rgb(247,247,247)'}" 
			:cell-style="{padding: '8px 0',borderColor: 'rgb(235,235,235)'}" 
			class="elTable" 
			:row-key="getRowKeys">
      <el-table-column type="index" header-align="center" align="center" width="80" label="序号">
        <template #default="scope">
          <span>{{ (pageIndex - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="考核名称" min-width="470" />
      <el-table-column prop="person_name" header-align="center" align="center" label="考核对象" min-width="140" />
      <el-table-column prop="status" header-align="center" align="center" label="状态" min-width="120" />
      <el-table-column prop="start_time" header-align="center" align="center" label="开始时间" min-width="175" />
      <el-table-column prop="end_time" header-align="center" align="center" label="结束时间" min-width="175" />
      <el-table-column header-align="center" align="center" width="80" label="操作">
        <template #default="scope">
          <el-dropdown>
            <span class="el-dropdown-link">
              <i class="el-icon-more cursor" style="font-size: 28px"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item :disabled="scope.row.status != '待审批'">
                  <a 
                    :class="{'a-disable': scope.row.status != '待审批'}"
                    @click="approvalHandle(scope.row)">
                    打分
                  </a>
                  <div class="mask" v-if="scope.row.status != '待审批'"></div>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination 
			@size-change="sizeChangeHandle" 
			@current-change="currentChangeHandle" 
			:current-page="pageIndex" 
			:page-sizes="[10, 20, 50]" 
			:page-size="pageSize" 
			:total="totalCount" 
			layout="total, sizes, prev, pager, next, jumper"
		></el-pagination>

  </div>
</template>

<script>
export default {
  data: function() {
    return {
      dataForm: {
        title: null
      },
      status: '待审批',
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      dataListLoading: false,
      dataListSelections: [],
      dataRule: {
        examName: [{ required: false, pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{1,10}$', message: '角色格式错误' }]
      }
    }
  },
  methods: {
    // 审批考核
    approvalHandle(row) {
      this.$router.push({
        name: 'ExamCheckInfo', 
        query: {
          id: row.id, 
          from: this.$route.name
        }
      })
    },
    // 查询功能
    searchHandle() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$refs['dataForm'].clearValidate()
          // 后端接收到''会报错，接收null是代表没有这个条件
          if (this.dataForm.title == '') {
            this.dataForm.title = null
          }
          if (this.pageIndex != 1) {
            this.pageIndex = 1
          }
          this.loadDataList()
        } else {
          return false
        }
      })
    },
    // 加载分页数据
    loadDataList() {
      this.dataListLoading = true
      let data = {
        title: this.dataForm.title,
        page: this.pageIndex,
        length: this.pageSize
      }
      if (this.status == '待审批') {
        // 为空是全查询，为1是只查询待审批
        data.status = 1
      } else if (this.status == '已完成') {
        data.status = 2
      }
      // console.log(data)
      this.$http('mark/selectMarkBypages', 'post', data, true, resp => {
        let page = resp.page
        console.log(page);
        this.dataList = page.list
        this.dataList.forEach(item => {
          if (item.status == 1) {
            item.status = '待审批'
          } else if (item.status == 2) {
            item.status = '已完成'
          } else if (item.status == 3) {
            item.status = '未开始'
          } else if (item.status == 4) {
            item.status = '已过时'
          }
          // 处理时间中间的T 如：2021-11-14T19:11:51
          item.start_time = item.start_time ? item.start_time.replace('T', ' ') : ''
          item.end_time = item.end_time ? item.end_time.replace('T', ' ') : ''
        })
        // console.log(this.dataList);
        
        this.totalCount = page.totalCount
        this.dataListLoading = false
      })
    },
    // 分页一页数量改变时触发
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.loadDataList()
    },
    // 分页换页时触发
    currentChangeHandle(val) {
      this.pageIndex = val
      this.loadDataList()
    },
    selectionChangeHandle: function(val) {
      this.dataListSelections = val
    },
    selectable: function(row, index) {
      // console.log(row)
      if (row.systemic || row.users > 0) {
        return false
      }
      return true
    },
    // 保存选中的数据id,row-key就是要指定一个key标识这一行的数据
    getRowKeys(row) {
      return row.id
    }
  },
  created() {
    this.loadDataList()
  }
}
</script>

<style lang="scss" scoped>
@import './examTask.scss';
</style>
